﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Module.Venues
{

    public class VenuesRepair1 : IHttpHandler
    {
        private int vId;
        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.QueryString["Type"];
            string html = context.Cache["venuesrepairform"].ToString();
            string strVId = context.Request.QueryString["VenuesId"];
            vId = int.Parse(strVId);
            BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
            Model.Module_Main module = BLLModule.GetModel(vId);
            BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            Jy123.Model.Public_JType pJType = BLLJType.GetModel(module.FJType.Value);
            int jFather = pJType.JFather.Value;
            bool isVenues = false;

            if (jFather == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.Venues_Main))
            {
                isVenues = true;
            }
            Jy123.BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
            Jy123.Model.Venues_Main venues = BLLVenues.GetModel(vId);
            Jy123.BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
            Jy123.Model.TCenter_Main TCenter = BLLTCenter.GetModel(vId);
            int peakValue = 100;//需要改进
            BLL.Public_Consume BLLConsume = new BLL.Public_Consume();
            List<Model.Public_Consume> listConsume = BLLConsume.GetModelList("FJType = " + module.FJType.Value.ToString());
            if (listConsume.Count > 0)
            {
                Model.Public_Consume consume = listConsume[0];
                peakValue = consume.PeakValue.Value;
            }
            Jy123.BLL.Public_Maintain BLLMaintain = new Jy123.BLL.Public_Maintain();
            string strWhere = string.Format("FJType = {0}", module.FJType.Value.ToString());
            List<Jy123.Model.Public_Maintain> listMaintain = BLLMaintain.GetModelList(strWhere);
            switch (type.ToLower())
            {
                case "load":
                    break;
                case "repair":
                    string strItems = context.Request.QueryString["Items"];
                    if (strItems.Length > 1)
                    {
                        Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                        Jy123.Model.User_Main user = BLLUser.GetModel(venues.FUser.Value);
                        int addPoint = 0;
                        string[] strArray = strItems.Split(',');
                        Dictionary<int, Jy123.Model.Public_Maintain> dicMaintain = new Dictionary<int, Jy123.Model.Public_Maintain>();
                        foreach (Jy123.Model.Public_Maintain maintian in listMaintain)
                        {
                            dicMaintain.Add(maintian.ID, maintian);
                        }

                        for (int i = 0; i < strArray.Length; i++)
                        {
                            if (!string.IsNullOrEmpty(strArray[i]))
                            {
                                Jy123.BLL.Public_MaintainRecord BLLRecord = new Jy123.BLL.Public_MaintainRecord();
                                Jy123.Model.Public_MaintainRecord record = new Jy123.Model.Public_MaintainRecord();
                                int id = int.Parse(strArray[i]);
                                Jy123.Model.Public_Maintain cMaintain = dicMaintain[id];
                                addPoint = addPoint + cMaintain.AddPoint.Value;
                                record.RepairTime = DateTime.Now;
                                record.FMaintain = cMaintain.ID;
                                record.FModule = module.ID;
                                BLLRecord.Add(record);
                            }
                        }
                        if (isVenues)
                        {
                            venues.Vitality = venues.Vitality.Value + addPoint;
                            if (venues.Vitality.Value > peakValue)
                            {
                                venues.Vitality = peakValue;
                            }
                            BLLVenues.Update(venues);
                        }
                        else
                        {
                            TCenter.Vitality = TCenter.Vitality.Value + addPoint;
                            if (TCenter.Vitality.Value > peakValue)
                            {
                                TCenter.Vitality = peakValue;
                            }
                            BLLTCenter.Update(TCenter);
                        }

                    }
                    break;
            }
            bool isNeedRepair = false;
            if (isVenues)
            {
                isNeedRepair = venues.Vitality.Value < peakValue;
            }
            else
            {
                isNeedRepair = TCenter.Vitality.Value < peakValue;
            }
            BindHtml(context, html, isNeedRepair, listMaintain);
        }

        private void BindHtml(HttpContext context, string html, bool isNeedRepair, List<Jy123.Model.Public_Maintain> listMaintain)
        {
            StringBuilder sbString = new StringBuilder();
            if (isNeedRepair)
            {
                foreach (Jy123.Model.Public_Maintain maintain in listMaintain)
                {
                    string strLi = context.Cache["venuesrepairli"].ToString();
                    strLi = strLi.Replace("$RepairImageSrc$", maintain.Model);
                    strLi = strLi.Replace("$CheckBoxId$", "checkbox" + maintain.ID.ToString());
                    strLi = strLi.Replace("$RepairName$", maintain.MName);
                    strLi = strLi.Replace("$AddPoint$", maintain.AddPoint.Value.ToString());
                    int cost = Convert.ToInt32(maintain.AddPoint.Value * maintain.Cost.Value);
                    strLi = strLi.Replace("$RepairCost$", cost.ToString());
                    strLi = strLi.Replace("$CheckBoxValue$", cost.ToString());
                    sbString.Append(strLi);
                }
                html = html.Replace("$Li$", sbString.ToString());
            }
            else
            {
                html = html.Replace("$Li$", "不必维护");
            }
            html = html.Replace("$RepairAllCost$", "0");
            context.Response.Write(html);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
